//answer.js


var app = getApp()
Page({
  data: {
    
    flag:1,        //评论按钮的状态 1：点赞前   0：点赞后
    color:1,      //底部栏点赞按钮状态
    heart:1,       //底部栏喜欢按钮状态
    star:1,        //收藏按钮
    flag01:1,   //举报按钮
    comment:1,   //评论
  },
  
  // ------------------数据加载模块 start--------------
  onLoad: function (options) {
    var answer = JSON.parse(options.answer);
    var qTitle = options.qTitle;
    //console.log(answer);
    this.setData({
      answer:answer,
      qTitle:qTitle,
      aid:answer.aid,
    })
    this.getAnswerComment();
  },
  onReachBottom: function() {
    this.getAnswerComment();  // 下拉重新获取用户评论
  },

  // ------------------数据加载模块 end--------------



  // ------------------功能模块 start--------------

  // 获取回答评论信息
  getAnswerComment:function(){
    var aid = this.data.aid;
    var mark = 'getAnswerComment'
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data:{
        aid:aid,
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var acList = res.data.data.acList;
        this.setData({
          answerCommentList:acList,
        })
        //console.log(this.data.answerCommentList)
      }
    });
  },

  inputAcComment:function(e){
    var acComment = e.detail.value;
    var aid = e.currentTarget.dataset.aid
    //console.log("acComment:"+acComment);
    //console.log("aid:"+aid);
    this.setData({
      acComment:acComment,
      aid:aid,
    })
    this.sendAcComment();
  },

  sendAcComment:function(){
    var mark = 'sendAcComment';
    var acComment = this.data.acComment;
    //console.log(acComment)
    var aid = this.data.aid;
    var userid =  getApp().globalData.userid;
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data:{
        mark:mark,
        acComment:acComment,
        aid:aid,
        userid:userid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var suc = res.data.data.suc;
        if(suc==1){
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '评论失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    });
  },

  //评论点赞
  reply:function(){
    if(this.data.flag){
      this.setData({
        flag:0,
        zanNum01:Number(this.data.zanNum01+1)
      })
    }else{
      this.setData({
        flag:1,
        zanNum01:Number(this.data.zanNum01-1)
      })
    }
    
  },

  //底部栏点赞
  // goodNumChange:function(){
  //   if(this.data.color){
  //     this.setData({
  //       color:0,
  //       goodNum:Number(this.data.goodNum+1)
  //     })
  //   }else{
  //     this.setData({
  //       color:1,
  //       goodNum:Number(this.data.goodNum-1)
  //     })
  //   }
    
  // },

  //底部栏喜欢按钮
  heartNumChange:function(){
    // console.log(this.data.answer.aLoveNum)
    if(this.data.heart){
      var mark = 'updateAnswerNumByqid';
      var mark_ = 'heartAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aLoveNum:this.data.answer.aLoveNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          heart: 0,
          answer:answer
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    }else{
      var mark = 'updateAnswerNumByqid01';
      var mark_ = 'heartAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aLoveNum:this.data.answer.aLoveNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          heart: 1,
          answer:answer
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    }
  },

  //底部栏收藏按钮
  starNumChange:function(){
    if(this.data.star){
      var mark = 'updateAnswerNumByqid';
      var mark_ = 'starAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aForkNum:this.data.answer.aForkNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          star: 0,
          answer:answer
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    }else{
      var mark = 'updateAnswerNumByqid01';
      var mark_ = 'starAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aForkNum:this.data.answer.aForkNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          star: 1,
          answer:answer
        })
      }
    })

        }
      })
    }
  },

  //举报
  dislikeNumChange:function(){
    if(this.data.flag01){
      var mark = 'updateAnswerNumByqid';
      var mark_ = 'dislikeAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aReportNum:this.data.answer.aReportNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          flag01: 0,
          answer:answer
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    }else{
      var mark = 'updateAnswerNumByqid01';
      var mark_ = 'dislikeAdd';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          mark_:mark_,
          aid: this.data.aid,
          aReportNum:this.data.answer.aReportNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getAnswerNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        aid: this.data.aid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var answer = res.data.data.answer;
        // console.log(aLoveNum)
        this.setData({
          flag01: 1,
          answer:answer
        })
      }
    })

        }
      })
    }
  },

  //点击评论图标弹出评论框
  discussAction:function(e){
    this.setData({
      discussShow:true
    })
  },

  //点击发送按钮收起评论框
  send:function(){
    var mark = 'updateAnswerNumByqid';
    var mark_ = 'discussAdd';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        mark_:mark_,
        aid: this.data.aid,
        aCommentNum:this.data.answer.aCommentNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        
  var mark = 'getAnswerNumByqid';
  wx.request({
    url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
    method: 'POST',
    data: {
      mark: mark,
      aid: this.data.aid,
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    },
    success: (res) => {
      var answer = res.data.data.answer;
      // console.log(aLoveNum)
      this.setData({
        discussShow:false,
        comment:0,
        answer:answer
      })
    }
  })

      }
    })
  },
  // ------------------功能模块 end--------------
})
